//
//  HWMyNoticeViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/1/16.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWMyNoticeViewController.h"
#import "HWMyNoticeCell.h"

@interface HWMyNoticeViewController()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSString *num;
@property (nonatomic, assign)BOOL hasNextPage;
@property (nonatomic, copy)NSArray *noticeArr;
@end

@implementation HWMyNoticeViewController

- (NSArray *)noticeArr
{
    if (!_noticeArr)
    {
        _noticeArr = [[NSArray alloc]init];
    }
    return _noticeArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
      
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        // 设置文字
        [header setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        header.stateLabel.font = [UIFont systemFontOfSize:17];
        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
        
        // 设置颜色
        header.stateLabel.textColor = LCHexColor(0x1f1f1f);
        header.lastUpdatedTimeLabel.textColor = [UIColor clearColor];
        
        // 设置刷新控件
        self.tableView.mj_header = header;
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewFootData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:17];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
        _tableView.mj_footer = footer;
    }
    return _tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buryPoint];
    [self setTitle:@"我的消息"];
    [self setDefaultNav];
    self.num = @"0";
    [self getNoticeInfoPageNum:@"0" pageSizes:@"10"];

    [self.view addSubview:self.tableView];
}

#pragma mark - UItableview delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.noticeArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70*kUIScaleBasePlus;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWMyNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoticeCell"];
    if (!cell)
    {
        cell = [[HWMyNoticeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NoticeCell"];
        cell.selectionStyle = 0;
    }
    
    NSDictionary *dic = self.noticeArr[indexPath.row];
    
    [cell getMyInformationdate:dic];
    
    return cell;
}

- (void)getNoticeInfoPageNum:(NSString *)pagenum pageSizes:(NSString *)pagesize

{
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"pageNum" : pagenum,
                               @"pageSize" : pagesize
                               };
    
    WeakSelf(self)
    [MyInfoManager setSystemList:prarmers sucess:^(id  _Nonnull acceptData) {
        weakSelf.noticeArr = [NSArray arrayWithArray:acceptData[@"list"]];
        if (weakSelf.noticeArr.count == 0)
        {
            weakSelf.tableView.mj_footer.hidden = YES;
        }
        weakSelf.hasNextPage = [acceptData[@"hasNextPage"] boolValue];
        [weakSelf.tableView reloadData];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"获取失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}


- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self getNoticeInfoPageNum:@"0" pageSizes:@"10"];
        weakSelf.num = @"0";
        [self.tableView.mj_header endRefreshing];
    });
}

- (void)loadNewFootData
{
    if (self.hasNextPage)
    {
        WeakSelf(self)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            weakSelf.num = [NSString stringWithFormat:@"%ld",weakSelf.num.integerValue+1];
            [weakSelf getNoticeInfoPageNum:weakSelf.num pageSizes:@"10"];
            [self.tableView.mj_footer endRefreshing];
        });
    }
    else
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableView.mj_footer endRefreshing];
            
        });
    }
    
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:@"/myNews"
                               fromId:nil
                                 toId:@"bd74eb421a8a4e0c"];
}


@end
